- @pagetitle = 'Create Token'
- type_options = Token::OPERATIONS.map { |a| "Token::#{a}".constantize.token_name.downcase }

.card
  .card-body
    %h3.mb-3= @pagetitle

    .row
      .col-12.col-md-10.col-lg-8
        - url = @token.new_record? ? tokens_path : token_path(@token)
        = form_with(model: @token, url: url, method: :post, local: true) do |f|
          .row
            .col
              .mb-3
                = f.label(:type, 'Type:', class: 'col-form-label me-2')
                .w-100.d-sm-none
                = f.collection_radio_buttons(:type, type_options, :to_s, :to_s,
                                             checked: f.object.type.nil? ? 'service' : f.object.type,
                                             class: 'form-check-input', include_hidden: false, required: true) do |radio|
                  .form-check.custom-control-inline
                    = radio.radio_button(class: 'form-check-input')
                    = radio.label(class: 'form-check-label')

          .row
            .col-sm
              .mb-3.ui-front
                = f.label(:description, 'Description:')
                = f.text_field(:description, class: 'form-control', placeholder: 'Eg: Rebuild vim package', maxlength: 64)
                .form-text.text-muted
                  Optional: provide a description to your token.

          .row#package-project-form
            .col-sm
              = render partial: 'webui/shared/search_box', locals: { html_id: 'project_name', label: 'Project name', required: false,
                                                                       data: { source: autocomplete_projects_path(local: true) } }
            .col-sm
              = render partial: 'webui/shared/search_box', locals: { html_id: 'package_name', label: 'Package name', required: false,
                                                                       data: { source: autocomplete_packages_path }, disabled: true }
            .col-12
              .mb-3.form-text.text-muted
                Optional: provide project and package names, to limit the token to a specific package.

          .row.d-none#fieldsets-token
            .col-12.col-md-10.col-lg-9
              .mb-3
                = f.label(:scm_token, 'SCM Token:')
                = render partial: 'webui/shared/required_label_mark'
                = f.password_field(:scm_token, size: 80, class: 'form-control', placeholder: 'Please enter your SCM token')
              .mb-3
                = f.label(:workflow_configuration_path, 'Path for Workflows Configuration File:')
                .input-group
                  = f.text_field(:workflow_configuration_path, value: '.obs/workflows.yml', class: 'form-control',
                                                               placeholder: 'Eg: .my_subdir/my_workflows_file.yml')
                .form-text.text-muted
                  The default path is '.obs/workflows.yml'.
              .mb-3
                = f.label(:workflow_configuration_url, 'URL to Workflows Configuration File:')
                .input-group
                  = f.text_field(:workflow_configuration_url, class: 'form-control',
                                                              placeholder: 'Eg: https://example.com/.my_subdir/my_workflows_file.yml')
                .form-text.text-muted
                  When the URL is given, the path will be ignored. The URL should be accessible for the OBS instance.
          .actions
            = link_to('Cancel', tokens_path, title: 'Cancel', class: 'btn btn-outline-secondary px-4 me-3 mt-3 mt-sm-0')
            = f.submit('Create', class: 'btn btn-primary px-4 mt-3 mt-sm-0')

- content_for :ready_function do
  :plain
    // Shows/Hide SCM Token input field
    $('input[type=radio]').on('change', function() {
      if (this.value == 'workflow') {
        $('#fieldsets-token').removeClass('d-none');
        $('#package-project-form').addClass('d-none');
        $('#token_scm_token').attr('required', true);
      }
      else if (!$('#fieldsets-token').attr('disabled')) {
        $('#fieldsets-token').addClass('d-none');
        $('#package-project-form').removeClass('d-none');
        $('#token_scm_token').attr('required', false);
      }
    });
